package org.dromara.oce.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.oce.config.JuLiangConfig;
import org.dromara.oce.config.JuLiangProperties;
import org.dromara.oce.constant.ReferralAppType;
import org.dromara.oce.constant.ReferralLinkType;
import org.dromara.oce.domain.bo.OceAdvertiserReferralRelationBo;
import org.dromara.oce.domain.dto.GetAdvertiserInfo;
import org.dromara.oce.domain.dto.OceAdvertiserReferralRelationDTO;
import org.dromara.oce.domain.dto.QueryAdvertiserInfoDTO;
import org.dromara.oce.domain.vo.OceAdvertiserReferralRelationVo;
import org.dromara.oce.service.OceAdvertiserReferralRelationService;
import org.dromara.oce.service.remote.OceanEngineRemoteService;
import org.dromara.oce.utils.PlatformParamTransUtil;
import org.slf4j.MDC;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 推广链接-广告主关联
 *
 * @author Lion Li
 * @date 2024-12-24
 * @module 管理平台接口
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oce/advertiserReferralRelation")
public class OceAdvertiserReferralRelationController extends BaseController {

    private final OceAdvertiserReferralRelationService oceAdvertiserReferralRelationService;
    private final OceanEngineRemoteService oceanEngineRemoteService;
    private final JuLiangConfig juLiangConfig;

    /**
     * 获取广告主详细信息
     *
     * @param thirdAdvertiserId 广告主第三方id
     */
    @SaCheckPermission("oce:advertiserReferralRelation:queryAdvertiserInfo")
    @GetMapping("/queryAdvertiserInfo")
    public R<QueryAdvertiserInfoDTO> queryAdvertiserInfo(@RequestParam("thirdAdvertiserId") String thirdAdvertiserId) {
        QueryAdvertiserInfoDTO resp = new QueryAdvertiserInfoDTO();
        GetAdvertiserInfo advertiserInfo = null;
        for (JuLiangProperties juLiangProperties : juLiangConfig.getJuLiangConfigList()) {
            MDC.put("appCode", juLiangProperties.getAppCode());
            advertiserInfo = oceanEngineRemoteService.getAdvertiserInfo(Long.valueOf(thirdAdvertiserId));
            if (advertiserInfo != null) {
                break;
            }
        }
        if(Objects.nonNull(advertiserInfo)) {
            resp.setThirdAdvertiserName(advertiserInfo.getName());
            resp.setThirdAdvertiserId(advertiserInfo.getId() + "");
            resp.setAppCode(advertiserInfo.getAppCode());
        }
        return R.ok(resp);
    }

    /**
     * 查询推广链接-广告主关联列表
     */
    @SaCheckPermission("oce:advertiserReferralRelation:list")
    @GetMapping("/list")
    public TableDataInfo<OceAdvertiserReferralRelationDTO> list(OceAdvertiserReferralRelationBo bo, PageQuery pageQuery) {
        TableDataInfo<OceAdvertiserReferralRelationDTO> tableDataInfo = new TableDataInfo<>();
        List<OceAdvertiserReferralRelationDTO> dtos = new ArrayList<>();
        Long deptId = LoginHelper.getDeptId();
        if(!LoginHelper.isSuperAdmin() && !LoginHelper.isTenantAdmin()){
            bo.setDeptId(deptId);
        }
        Long deptIdCache = PlatformParamTransUtil.getDynamicDept();
        if (deptIdCache > 0L) {
            bo.setDeptId(deptIdCache);
        }
        TableDataInfo<OceAdvertiserReferralRelationVo> dataInfo = oceAdvertiserReferralRelationService.queryPageList(bo, pageQuery);
        List<OceAdvertiserReferralRelationVo> list = dataInfo.getRows();
        if(CollectionUtils.isNotEmpty(list)) {
            for (OceAdvertiserReferralRelationVo vo : list) {
                OceAdvertiserReferralRelationDTO dto = new OceAdvertiserReferralRelationDTO();
                BeanUtil.copyProperties(vo, dto);
                dto.setAppTypeDesc(ReferralAppType.getDescByType(dto.getAppType()));
                dto.setReferralLinkTypeDesc(ReferralLinkType.getDescByType(dto.getReferralLinkType()));
                dtos.add(dto);
            }
        }
        tableDataInfo.setCode(dataInfo.getCode());
        tableDataInfo.setMsg(dataInfo.getMsg());
        tableDataInfo.setRows(dtos);
        tableDataInfo.setTotal(dataInfo.getTotal());
        return tableDataInfo;
    }

    /**
     * 获取推广链接-广告主关联详细信息
     *
     * @param advertiserReferralId 主键
     */
    @SaCheckPermission("oce:advertiserReferralRelation:query")
    @GetMapping("/{advertiserReferralId}")
    public R<OceAdvertiserReferralRelationVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long advertiserReferralId) {
        return R.ok(oceAdvertiserReferralRelationService.queryById(advertiserReferralId));
    }

    /**
     * 新增推广链接-广告主关联
     */
    @SaCheckPermission("oce:advertiserReferralRelation:add")
    @Log(title = "推广链接-广告主关联", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody OceAdvertiserReferralRelationBo bo) {
        return toAjax(oceAdvertiserReferralRelationService.insertByBo(bo));
    }

    /**
     * 修改推广链接-广告主关联
     */
    @SaCheckPermission("oce:advertiserReferralRelation:edit")
    @Log(title = "推广链接-广告主关联", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody OceAdvertiserReferralRelationBo bo) {
        return toAjax(oceAdvertiserReferralRelationService.updateByBo(bo));
    }

    /**
     * 删除推广链接-广告主关联
     *
     * @param advertiserReferralIds 主键串
     */
    @SaCheckPermission("oce:advertiserReferralRelation:remove")
    @Log(title = "推广链接-广告主关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{advertiserReferralIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] advertiserReferralIds) {
        return toAjax(oceAdvertiserReferralRelationService.deleteWithValidByIds(List.of(advertiserReferralIds), true));
    }
}
